package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxuwz.medical.domain.area.Area;
import com.gxuwz.medical.domain.chronicdis.Chronicdis;
import com.gxuwz.medical.vo.PageBean;

public class ChronicdisServlet extends HttpServlet {


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String method = request.getParameter("method");
			try {
				if(method.equals("list")){
					list(request,response);
				}else if(method.equals("add")){
					add(request,response);
				}else if(method.equals("del")){
					del(request,response);
				}else if(method.equals("edit")){
					edit(request,response);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	

	public void jump(HttpServletRequest request, HttpServletResponse response,String path) throws ServletException, IOException, SQLException{
		Chronicdis c = new Chronicdis();
		String page = request.getParameter("page");
		if (page == null)
			page = "1";
		int currentpage = Integer.parseInt(page);
		PageBean pageBean = c.queryAll(currentpage, 5);
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher(path).forward(request, response);
	}
	
	public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
		String path = "/page/chronicdis/chronicdis_list.jsp";
		Chronicdis c = new Chronicdis();
		String page = request.getParameter("page");
		if (page == null)
			page = "1";
		int currentpage = Integer.parseInt(page);
		PageBean pageBean = c.queryAll(currentpage, 5);
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher(path).forward(request, response);
	}
	
	public void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		String path = "/page/chronicdis/chronicdis_list.jsp";
		String msg = "添加失败";
		String illcode = request.getParameter("illcode");
		String illname = request.getParameter("illname");
		Chronicdis c = new Chronicdis();
		c.setIllcode(illcode);
		c.setIllname(illname);
		c.add();
		jump(request,response,path);
		
	}

	public void del(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		String path = "/page/chronicdis/chronicdis_list.jsp";
		String illcode = request.getParameter("illcode");
		illcode = new String(illcode.getBytes("ISO-8859-1"),"UTF-8");
		Chronicdis c = new Chronicdis();
		c.delete(illcode);
		jump(request,response,path);
	}
	
	public void edit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		String path = "/page/chronicdis/chronicdis_list.jsp";
		String illcode = request.getParameter("illcode");
		String illname = request.getParameter("illname");
		Chronicdis c = new Chronicdis();
		c.setIllcode(illcode);
		c.setIllname(illname);
		c.update();
		jump(request,response,path);
	}
}
